package com.jia.product.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice(basePackages = "com.jia.product")
public class GuliMallExceptionControllerAdvice {

    Logger logger = LoggerFactory.getLogger(GuliMallExceptionControllerAdvice.class);

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public void exception(MethodArgumentNotValidException e) {
        BindingResult bindingResult = e.getBindingResult();
        bindingResult.getFieldErrors().forEach(error ->
                logger.error("实体信息：{}，异常信息：{}", error.getClass(), error.getField() + ":" + error.getDefaultMessage()));
    }

    @ExceptionHandler(value = Exception.class)
    public void exceptionHandler(Exception e) throws Exception {
        logger.error("异常信息：{},异常类型：{}", e.getMessage(), e.getClass());
    }

}
